package com.itheima.youdianda.controller;


import com.itheima.youdianda.common.BaseContext;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.entity.YddArticle;
import com.itheima.youdianda.entity.YddUser;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddUserService;
import com.itheima.youdianda.util.MyPages;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-24
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class YddUserController {
	@Autowired
	private YddUserService yddUserService;

	@Autowired
	private YddArticleService yddArticleService;

	@GetMapping("/myArticle")
	public R<MyPages<YddArticle>> myArticle(@RequestParam(defaultValue = "1") String page,@RequestParam(defaultValue = "10") String limit){
		log.info("/user/myArticle,page:{},limit:{}",page,limit);
		MyPages<YddArticle> myPages = yddArticleService.getMyArticleByPage(page, limit);
		return R.success(myPages);
	}

	@GetMapping("/getUserInfo")
	public R<Map<String,Object>> getUserInfo(){
		log.info("/user/getUserInfo");
		Map<String,Object> map = yddUserService.getUserInfo();
		return R.success(map);
	}

	@GetMapping("/userDataList")
	public R<Map<String,Object>> userDataList(@RequestParam(defaultValue = "1") String page,@RequestParam(defaultValue = "10") String limit, String type){
		log.info("/user/userDataList,page:{},limit:{},type:{}",page,limit,type);
		Map<String,Object> map = yddUserService.userDataList(page, limit, type);
		return R.success(map);
	}

	@PostMapping("/addArticle")
	public R<String> addArticle(@RequestBody YddArticle yddArticle){
		log.info("/user/addArticle,yddArticle:{}",yddArticle);
		yddArticle.setUser_id(Integer.parseInt(String.valueOf(BaseContext.getCurrentId())));
		yddArticleService.save(yddArticle);
		return R.success("");
	}

	@GetMapping("/delArticle")
	public R<String> delArticle(@RequestParam String id){
		log.info("/user/delArticle,id:{}",id);
		yddArticleService.removeById(id);
		return R.success("删除成功!");
	}

	@PostMapping("/editArticle")
	public R<String> editArticle(@RequestBody YddArticle yddArticle){
		log.info("/user/editArticle,yddArticle:{}",yddArticle);
		yddArticleService.updateById(yddArticle);
		return R.success("");
	}

	@GetMapping("/userDataHandle")
	public R<String> userDataHandle(String type, String action, String article_id){
		log.info("/user/userDataHandle,type:{},action:{},article_id:{}",type,action,article_id);
		yddArticleService.userDataHandle(type, action, article_id);
		return R.success("操作成功!");
	}

	@PostMapping("/update")
	public R<String> update(@RequestBody YddUser yddUser){
		log.info("/user/update,yddUser:{}",yddUser);
		yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes()));
		yddUserService.updateById(yddUser);
		return R.success("修改成功!");
	}
}
